// pages/Main/mainIndex/mainIndex.js
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
var common = require('../../../utils/common.js')
let bbb=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    testList: [],
    orgid: "",
    userid: "",
    title: '',
    ur: '',
    filed: ',,,,',
    num: 1,
    scrollHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let c = common.Getorgid();
    var scrollHeight = scrollHeight;
    wx.getSystemInfo({
      success: function (res) {
        scrollHeight = res.windowHeight
      }
    });  
    if (c.role == "学生") {
      this.setData({
        orgid: c.orgid,
        userid: c.userid,
        scrollHeight: scrollHeight,
      });
    }
    else {
      this.setData({
        orgid: c.orgid,
        userid: options.id,
        scrollHeight: scrollHeight,
      });
    }
    this.QueryHasParticipated();
    this.shanxuan();
  },
  stuTestall: function (event) {
    console.log(this.data.testList);
    console.log(event);
    wx.navigateTo({
      url: "../stuTestall/stuTestall?type=" + event.currentTarget.dataset.type + "&&parid=" + event.currentTarget.dataset.parid + "&&examid=" + event.currentTarget.dataset.examid
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.ConditionList = this.selectComponent("#ConditionList");
    this.ConditionList.close();
  },
  isShow: function (e) {
    this.setData({
      testList: [],
      num: 1
    })
    this.ConditionList.isShow();
    this.QueryHasParticipated();
  },
  //筛选
  shanxuan: function (ur) {
    this.setData({
      testList: [],
      num: 1
    })
    if (ur == '' || ur == undefined) {
      var ur = '';
      let params = {
        "classid": "5b3bbf31-44be-483b-93c5-7229c486b85d", "orgid": this.data.orgid, "ur": ur
      }
      businessLogic.getCondition(params, (res) => {
        //渲染页面
        this.ConditionList.conditionList(res, ur);
      })
    } else {
      console.log(ur.detail)
      let params = {
        "classid": "5b3bbf31-44be-483b-93c5-7229c486b85d", "orgid": this.data.orgid, "ur": ur.detail[0]
      }
      businessLogic.getCondition(params, (res) => {
        //渲染页面
        this.setData({
          ur: ur.detail[0],
          filed: ur.detail[1]
        })
        this.ConditionList.conditionList(res, ur.detail[0]);

      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, 
  bindDownLoad: function () {
    if (this.data.lenpage > this.data.num) {
      var di = this.data.num;
      var di = di + 1;
      this.setData({
        num: di
      });
      console.log(di)
      this.QueryHasParticipated();
    }
    else {
      common.showToast("已加载全部", "loading");
    }

  },  
  //8.4.2 查询已参加考试试卷列表
  QueryHasParticipated: function () {
    common.showToast("加载中", "loading");
    let params = { "page": this.data.num, "num": 15, "orgid": this.data.orgid, "userid": this.data.userid, searchField: "peopleid,examid|考试系统模型|title,kenids|数据词典|title,subject|数据词典|title,difficulty|数据词典|title,semester|数据词典|title,iscomplete", "searchValue": this.data.userid + "," + this.data.title + "," + this.data.filed+",1" }
    businessLogic.QueryHasParticipated(params, (res) => {
      // console.log(params)
      //console.log(res);
      var page = res.page
      var res = res.infos;
      for (let a = 0; a < res.length; a++) {
        var x = []
        switch (res[a].statemark) {
          case 1:
            x = "已批改"
            break;
          default:
            x = "未批改";
        }
        res[a].statemarkCN = []
        res[a].statemarkCN.push(x);
        switch (res[a].subject_dictionary_title) {
          case "语文":
            x = "yuwen"
            break;
          case "数学":
            x = "shuxue"
            break;
          case "英语":
            x = "english"
            break;
          case "物理":
            x = "wuli"
            break;
          case "化学":
            x = "huaxue"
            break;
          case "政治":
            x = "zhengzhi"
            break;

          default:
            x = "zhengzhi";
        }
        res[a].color = [];
        res[a].color.push(x);

      }
      bbb = this.data.testList
      for (var a = 0; a < res.length; a++) {
        bbb.push(res[a]);
      } 
      this.setData({
        testList: bbb,
        lenpage: page.pages
      });
    });
  },
  //搜索
  bindKeyInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  search2: function (event) {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, searchField: "peopleid,examid|考试系统模型|title,kenids|数据词典|title,subject|数据词典|title,difficulty|数据词典|title,semester|数据词典|title", "searchValue": this.data.userid + "," + this.data.title + "," + this.data.filed }
    businessLogic.QueryHasParticipated(params, (res) => {
      // console.log(params)
      // console.log(res);
      var res = res.infos;
      for (let a = 0; a < res.length; a++) {
        var x = []
        switch (res[a].statemark) {
          case 1:
            x = "已批改"
            break;
          default:
            x = "未批改";
        }
        res[a].statemarkCN = []
        res[a].statemarkCN.push(x);
        switch (res[a].subject_dictionary_title) {
          case "语文":
            x = "yuwen"
            break;
          case "数学":
            x = "shuxue"
            break;
          case "英语":
            x = "english"
            break;
          case "物理":
            x = "wuli"
            break;
          case "化学":
            x = "huaxue"
            break;
          case "政治":
            x = "zhengzhi"
            break;

          default:
            x = "zhengzhi";
        }
        res[a].color = [];
        res[a].color.push(x);

      }
      this.setData({
        testList: res
      });
    });
  },
})